SuperStrict

Framework maxgui.drivers
Import brl.eventqueue
Import klepto.scintilla


'**************************Sample***************************
Local window:TGadget
Local textarea:TScintillaTextArea ' TGadget works as well, but then we need to cast when using scintilla specific stuff

Window = CreateWindow("Lua Sample Editor", 130, 20, 800, 600,, 15 | WINDOW_ACCEPTFILES)
Local tabber:TGadget
Local document:TGadget

Local filemenu:TGadget
Local editmenu:TGadget
Local editmenuPOP:TGadget
Local helpmenu:TGadget

Const MENU_NEW:Int=101
Const MENU_OPEN:Int=102
Const MENU_SAVE:Int=103
Const MENU_CLOSE:Int=104
Const MENU_EXIT:Int=105

Const MENU_CUT:Int=106
Const MENU_COPY:Int=107
Const MENU_PASTE:Int = 108
Const MENU_UNDO:Int = 109
Const MENU_REDO:Int = 110
Const MENU_FONT:Int = 111

Const MENU_ABOUT:Int=112


filemenu=CreateMenu("&File",0,WindowMenu(window))
CreateMenu"&New",MENU_NEW,filemenu,KEY_N,MODIFIER_COMMAND
CreateMenu"&Open",MENU_OPEN,filemenu,KEY_O,MODIFIER_COMMAND
CreateMenu"&Close",MENU_CLOSE,filemenu,KEY_W,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"&Save",MENU_SAVE,filemenu,KEY_S,MODIFIER_COMMAND
CreateMenu"",0,filemenu
CreateMenu"E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND

editmenu = CreateMenu("&Edit" , 0 , WindowMenu(window) )
CreateMenu "&Undo",MENU_UNDO,editmenu,KEY_U,MODIFIER_COMMAND
CreateMenu "&Redo",MENU_REDO,editmenu,KEY_R,MODIFIER_COMMAND
CreateMenu"",0,editmenu
CreateMenu "Cu&t",MENU_CUT,editmenu,KEY_X,MODIFIER_COMMAND
CreateMenu "&Copy",MENU_COPY,editmenu,KEY_C,MODIFIER_COMMAND
CreateMenu "&Paste" , MENU_PASTE , editmenu , KEY_V , MODIFIER_COMMAND
CreateMenu "Font",MENU_FONT,editmenu

editmenuPop = CreateMenu("&Edit" , 0 , Null)
CreateMenu "&Undo",MENU_UNDO,editmenuPop ,KEY_U,MODIFIER_COMMAND
CreateMenu "&Redo",MENU_REDO,editmenuPop ,KEY_R,MODIFIER_COMMAND
CreateMenu"",0,editmenuPop 
CreateMenu "Cu&t",MENU_CUT,editmenuPop ,KEY_X,MODIFIER_COMMAND
CreateMenu "&Copy",MENU_COPY,editmenuPop ,KEY_C,MODIFIER_COMMAND
CreateMenu "&Paste",MENU_PASTE,editmenuPop ,KEY_V,MODIFIER_COMMAND

helpmenu=CreateMenu("&Help",0,WindowMenu(window))
CreateMenu "&About",MENU_ABOUT,helpmenu

UpdateWindowMenu window

textarea = CreateScintillaTextArea(0,0,ClientWidth(window),ClientHeight(window),window)
SetGadgetLayout textarea , 1 , 1 , 1 , 1
SetGadgetLayout textarea, 1, 1, 1, 1
ActivateGadget textarea

'Highlighting stuff
Local luaConfig:TScintillaConfig = TScintillaConfig.Create(textarea, SCLEX_LUA)
luaConfig.SetStyle(SCE_LUA_DEFAULT,, [0, 0, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_COMMENT,, [0, 128, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_COMMENTLINE,, [0, 128, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_COMMENTDOC,, [128, 128, 128], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_NUMBER,, [0, 128, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_WORD,, [0, 0, 255], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_STRING,, [128, 0, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_CHARACTER,, [128, 0, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_LITERALSTRING,, [128, 0, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_PREPROCESSOR,, [255, 0, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_OPERATOR,, [0, 0, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_IDENTIFIER,, [0, 0, 0], [255, 255, 255])
luaConfig.SetStyle(SCE_LUA_STRINGEOL,, [0, 0, 0], [255, 255, 255])
luaConfig.Setkeywords(0, LoadKeywords("luacommands.txt"))
luaConfig.SetCaret([0, 0, 0], CARETSTYLE_LINE)
luaConfig.setProperty("fold", "1")
luaConfig.setProperty("fold.compact", "0")
luaConfig.Setindentationguides(SC_IV_REAL)
luaConfig.Update()

textarea.SetLineNumbering(0, True)
textarea.SetMarginTypen(1, SC_MARGIN_SYMBOL)
textarea.SetMarginTypen(2, SC_MARGIN_SYMBOL)
textarea.SETMARGINWIDTHN(1, 20)
textarea.SETMARGINWIDTHN(2, 20)
textarea.Setmarginsensitiven(1, True)
textarea.SetMarginSensitiven(2, True)
textarea.UsePopup(0) 'Deactivate Scintillas own popup
textarea.Setmarginmaskn(2, SC_MASK_FOLDERS) ;
textarea.Setmarginmaskn(1, ~SC_MASK_FOLDERS) ;
textarea.Markerdefine(SC_MARKNUM_FOLDER, SC_MARK_CIRCLEPLUS)
textarea.Markerdefine(SC_MARKNUM_FOLDEROPEN, SC_MARK_CIRCLEMINUS)
textarea.Markerdefine(SC_MARKNUM_FOLDEREND, SC_MARK_CIRCLEPLUSCONNECTED)
textarea.Markerdefine(SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNERCURVE)
textarea.Markerdefine(SC_MARKNUM_FOLDEROPENMID, SC_MARK_CIRCLEMINUSCONNECTED)
textarea.Markerdefine(SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE)
textarea.Markerdefine(SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNERCURVE)
textarea.Setfoldflags(16)

textarea.Markersetback(SC_MARKNUM_FOLDER, 0, 0, 0)
textarea.Markersetback(SC_MARKNUM_FOLDEROPEN, 0, 0, 0)
textarea.Markersetback(SC_MARKNUM_FOLDEREND, 0, 0, 0)
textarea.Markersetback(SC_MARKNUM_FOLDERMIDTAIL, 0, 0, 0)
textarea.Markersetback(SC_MARKNUM_FOLDEROPENMID, 0, 0, 0)
textarea.Markersetback(SC_MARKNUM_FOLDERSUB, 0, 0, 0)
textarea.Markersetback(SC_MARKNUM_FOLDERTAIL, 0, 0, 0)

textarea.Markersetfore(SC_MARKNUM_FOLDER, 255, 255, 255)
textarea.Markersetfore(SC_MARKNUM_FOLDEROPEN, 255, 255, 255)
textarea.Markersetfore(SC_MARKNUM_FOLDEREND, 255, 255, 255)
textarea.Markersetfore(SC_MARKNUM_FOLDERMIDTAIL, 255, 255, 255)
textarea.Markersetfore(SC_MARKNUM_FOLDEROPENMID, 255, 255, 255)
textarea.Markersetfore(SC_MARKNUM_FOLDERSUB, 255, 255, 255)
textarea.Markersetfore(SC_MARKNUM_FOLDERTAIL, 255, 255, 255)

textarea.Markerdefine(0, SC_MARK_CIRCLE)
textarea.Markersetback(0, 255, 0, 0)
textarea.Markeradd(22, 0)

SetTextAreaText(textarea, LoadText("sample.lua"))
Local keylist:String[] = LoadKeywords("luacommands.txt").split(" ")

Local font:TGuiFont = Null

While WaitEvent()
'	Print CurrentEvent.toString() + " " + EventSourceHandle()
	Select EventID()
		Case EVENT_GADGETMENU
			If EventSource() = textarea Then
				PopupWindowMenu(Window, editmenuPOP)
			End If
		Case EVENT_WINDOWCLOSE
			End
		Case EVENT_APPTERMINATE
			End
		Case EVENT_SCINTILLA
			'Removed Scintilla Eventhandling from main code now everything needs to be handled in the
			'maxgui eventsystem
			
			If EventSource() = textarea Then
				Local n:TScintillaEventData = TScintillaEventData(EventExtra())
			'	Print ScintillaToString(n.code)
				Select n.Code
					Case SCN_MARGINCLICK
						Local Line:Int = textarea.LineFromPosition(n.position)
						If n.margin = 2 Then
							Local levelClick:Int = textarea.GETFOLDLEVEL(line)
							If levelClick & SC_FOLDLEVELHEADERFLAG <> 0 Then
								textarea.Togglefold(Line)
							End If
						Else If n.margin = 1 Then
							If textarea.Markerget(Line) > 0 Then
									textarea.MARKERDELETE(line,0)
								Else
									textarea.MARKERADD(line,0)
							End If
								
						End If
					
					Case SCN_MODIFIED
					Case SCN_CHARADDED
						PerformIntellitrace(textarea, n, keylist)
				End Select
				
			End If
			
		Case EVENT_MENUACTION
			Select EventData()
				Case MENU_NEW
					SetTextAreaText(textarea,"")
				Case MENU_OPEN
					Local File:String = RequestFile("Load Lua file", "Lua Files:lua")
					If FileType(File) = 1 Then SetTextAreaText(textarea,LoadText(File))
				Case MENU_UNDO
					textarea.Undo()
				Case MENU_REDO
					textarea.Redo()
				Case MENU_CUT
					textarea.Cut()
				Case MENU_COPY
					textarea.Copy()
				Case MENU_PASTE
					textarea.paste()
				Case MENU_FONT
					font = RequestFont(font)
					If font
					SetGadgetFont textarea, font
					luaConfig.SetStyle(SCE_LUA_DEFAULT, font, [0, 0, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_COMMENT, font, [0, 128, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_COMMENTLINE, font, [0, 128, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_COMMENTDOC, font, [128, 128, 128], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_NUMBER, font, [0, 128, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_WORD, font, [0, 0, 255], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_STRING, font, [128, 0, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_CHARACTER, font, [128, 0, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_LITERALSTRING, font, [128, 0, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_PREPROCESSOR,, [255, 0, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_OPERATOR, font, [0, 0, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_IDENTIFIER, font, [0, 0, 0], [255, 255, 255])
					luaConfig.SetStyle(SCE_LUA_STRINGEOL, font, [0, 0, 0], [255, 255, 255])
					luaConfig.Update()
				   	 EndIf
				Case MENU_EXIT
					End
				Case MENU_ABOUT
					Notify "Scintilla~nBlitzmaxGadget"
			End Select
	End Select
Wend

Function PerformIntellitrace(textarea:TScintillaTextArea, scn:TScintillaEventData, keywords:String[])
	Local curPosition:Int = TextAreaCursor(textarea)
	Local s:Int = textarea.Wordstartposition(curPosition, 1)
	Local e:Int = textarea.Wordendposition(curPosition, 1)
	Local l:Int = 0
	Local token:String
	
	Try
		token = TextAreaText(textarea, s, (e - s) , TEXTAREA_CHARS)
		l=token.length
	Catch ex:String
		Print "Exception: " + ex
	End Try
	
	Print token + " -- " + l
	Local count:Int = 0
	If token.Length > 0 And textarea.Autocactive() = 0 Then
		Local keylist:String = ""
		For Local T:String = EachIn keywords
			If T.ToLower().StartsWith(token.toLower()) Then
				keylist:+ t + " "
				count:+1
			End If
		Next
		textarea.Autocsetignorecase(1)
		textarea.Autocsetmaxheight(10)
		textarea.Autocshow(l, keylist)
	End If
End Function




